package com.excel.read;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import com.excel.comp.ButtonComp;
import com.excel.comp.InputComp;
import com.excel.comp.LabelComp;

public abstract class HTMLComp {
	public static final int INPUT_TYPE = 1;
	public static final int BTN_TYPE = 2;
	public static final int LABEL_TYPE = 3;
	public static final int HIDDEN_TYPE = 4;
	public int type;
	public String name;
	public String id;
	public HashMap paramMap;
	
	/**
     * 物理表名
     */
    public String tableName;
	
	public abstract String getHTML();
	
	public String getParameters() {
		StringBuffer str = new StringBuffer();
		Iterator iter = paramMap.keySet().iterator();
		while(iter.hasNext()) {
			String paramKey = (String) iter.next();
			if(!paramKey.equals("id")) {
			String value = (String) paramMap.get(paramKey);			
			str.append(" "+paramKey + "=" + value +" ");
			}
		}
		str.append(" id=" + this.tableName+ "."+paramMap.get("id") +" ");
		str.append(" name=" + this.tableName+ "."+paramMap.get("id") +" ");
		return str.toString();
	}
	
	/**
	 * 取得script 名字， 值语句
	 * @param type
	 * @return
	 */
	public String getVarStatement() {
		String varState = "";
		String value = (String) "document.getElementById('"+(this.tableName+ "."+paramMap.get("id"))+"').value";
		
		varState =  "var "+this.tableName+ "_"+paramMap.get("id")+"="+value+";";
		return varState;
	}
	
	public String getVarName() {
		String varState = "";
		String value = (String) paramMap.get("value");
		if(value == null) {
			value = null;
		}
		varState =  this.tableName+ "_"+paramMap.get("id");
		return varState;
	}
	
	public String getVar() {
		String varState = "";
		String value = (String) paramMap.get("value");
		if(value == null) {
			value = null;
		}
		varState =  this.tableName+ "."+paramMap.get("id");
		return varState;
	}
	
	
	public String getCompName() {
		String varState = "";
		String value = (String) paramMap.get("value");
		if(value == null) {
			value = null;
		}
		varState =  this.tableName+ "."+paramMap.get("id");
		return varState;
	}
	
	public String getVarValue() {
		String varState = "";
		String value = (String) paramMap.get("value");
		if(value == null) {
			value = null;
		}
		varState =  value;
		return varState;
	}
	
	
	public int getType(String type) {
		int aType = this.INPUT_TYPE;
		int aIndex = type.indexOf("[");
		if(aIndex == -1) {
			type = type.substring(0, type.length());
		}else {
			type = type.substring(0, aIndex);
		}
		if(type==null || type.equals("input")) {
			aType = this.INPUT_TYPE;
		}else if(type.equals("btn")) {
			aType = this.BTN_TYPE;
		}else if(type.equals("hidden")) {
			aType = this.HIDDEN_TYPE;
		}else {
			aType = this.LABEL_TYPE;
		}
		return aType;
	}
	
	public static HashMap getParameters(String type) {
		HashMap hm = new HashMap();
		int aIndex = type.indexOf("[");
		if(aIndex >-1) {
			String param = type.substring(aIndex+1, type.lastIndexOf("]"));
			String params [] = param.split("&");
			for(int i=0 ; i<params.length; i++) {
				String paramStr = params[i];
				String []nameValues = paramStr.split("=");
				String aName = nameValues[0];
				String aValue = nameValues[1];
				hm.put(aName, aValue);
			}
		}
		return hm;
	}
	public HTMLComp getComp(String type) {
		int aType = getType(type);
		
		HTMLComp aComp = null;
		switch(aType) {
		case INPUT_TYPE:
			aComp = new InputComp();
			HashMap hm = getParameters(type);
			aComp.setParamMap(hm);
			aComp.type = INPUT_TYPE;
			break;
		case LABEL_TYPE:
			aComp = new LabelComp();
			HashMap hm2 = getParameters(type);
			aComp.name = type;
			aComp.type = LABEL_TYPE;
			aComp.setParamMap(hm2);
			break;
		case BTN_TYPE:
			aComp = new ButtonComp();
			HashMap hm3 = getParameters(type);
			aComp.name = type;
			aComp.type = BTN_TYPE;
			aComp.setParamMap(hm3);
			break;		
			
		}
		return aComp;
		
	}
	public String[] getTypes(String types) {
		 String[] ms = types.split("@");	
		 if(ms.length>1) {
			 int k = 0;
			 k = 100;
		 }
		 return ms;
	}
	public List<HTMLComp> getComps(String type) {
		
		HTMLComp aComp = null;
		List<HTMLComp> aComps = new ArrayList();
		String[] ms = getTypes(type);
		for(int i=0; i<ms.length; i++) {
			int aType = getType(ms[i]);
			switch(aType) {
			case INPUT_TYPE:
				aComp = new InputComp();
				HashMap hm = getParameters(ms[i]);
				aComp.setParamMap(hm);
				aComp.type = INPUT_TYPE;
				aComps.add(aComp);
				break;
			case LABEL_TYPE:
				aComp = new LabelComp();
				HashMap hm2 = getParameters(ms[i]);
				aComp.name = type;
				aComp.type = LABEL_TYPE;
				aComp.setParamMap(hm2);
				aComps.add(aComp);
				break;
			case BTN_TYPE:
				aComp = new ButtonComp();
				HashMap hm3 = getParameters(ms[i]);
				aComp.name = type;
				aComp.type = BTN_TYPE;
				aComp.setParamMap(hm3);
				aComps.add(aComp);
				break;		
				
			}			
		}	
		return aComps;
		
	}
	public HashMap getParamMap() {
		return paramMap;
	}

	public void setParamMap(HashMap paramMap) {
		this.paramMap = paramMap;
	}

	public String getFieldName() {
		return (String) paramMap.get("id");
	}

	
	
	
	
}
